//
//  CNTabbar.m
//  CNKICommunity
//
//  Created by wei on 17/1/23.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "CNTabbar.h"
#import "CNKIPublishView.h"
@interface CNTabbar()<PublishButtonClickDelegate>

//发布按钮
@property(nonatomic,weak) UIButton *PublishBtn;
//发布页面
@property(nonatomic,strong)CNKIPublishView *PublishView;
//小红点
@property(nonatomic,weak) UIView *RedView;
@end


@implementation CNTabbar
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIButton *tabbarbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [tabbarbtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [tabbarbtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        tabbarbtn.size = tabbarbtn.currentBackgroundImage.size;
        [tabbarbtn addTarget:self action:@selector(clickPublishBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tabbarbtn];
        self.PublishBtn = tabbarbtn;
        
        UIView *customRedView = [[UIView alloc]init];
        customRedView.size = CGSizeMake(8, 8);
        customRedView.backgroundColor = [UIColor redColor];
        customRedView.layer.cornerRadius = 4;
        customRedView.layer.masksToBounds = YES;
        [self addSubview:customRedView];
        self.RedView = customRedView;
        
        
        
    }
    return self;
    
}
-(void)clickPublishBtn
{
   //弹出发布自定义view
    NSLog(@"点击了发布按钮");
    CNKIPublishView *newView = [[CNKIPublishView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    newView.backgroundColor = [UIColor lightGrayColor];
    newView.ButtonClickDelegate = self;
    [[[UIApplication sharedApplication] keyWindow] addSubview:newView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelPage)];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    [newView addGestureRecognizer:tap];
    
    _PublishView = newView;
}
-(void)cancelPage
{
    [self cancelCompleteBlock:nil];
}
-(void)cancelCompleteBlock:(void(^)())complete
{
    //回调block
    [_PublishView removeFromSuperview];
    !complete ? : complete();
}

#pragma mark - PublishButtonClickDelegate
-(void)ClickButtontag:(NSInteger)buttonTag
{
  
    if (buttonTag == 10)
    {
        [self cancelPage];
    }
    [self cancelCompleteBlock:^{
       
       //此处完善跳转内容
       NSLog(@"点击了第%ld个按钮，进入发布页面",(long)buttonTag);
   }];
}

//重新布局tabbarBtn
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = self.width / 5;
    CGFloat btnH = self.height;
    NSInteger index = 0;
    
    
    self.PublishBtn.center = CGPointMake(self.width / 2, self.height / 2);
    if ([WYTDevicesTool iPhone5_iPhone5s_iPhone5c])
    {
       self.RedView.center = CGPointMake(self.width / 2 + btnW + btnW / 5, self.height / 2 - 20);
    }
    else if ([WYTDevicesTool iPhone6_iPhone6s])
    {
       self.RedView.center = CGPointMake(self.width / 2 + btnW + btnW / 5, self.height / 2 - 20);
    }
    else if ([WYTDevicesTool iPhone6Plus_iPhone6sPlus])
    {
        self.RedView.center = CGPointMake(self.width / 2 + btnW + btnW / 5 - 5, self.height / 2 - 20);
    }
    else
    {
        self.RedView.center = CGPointMake(self.width / 2 + btnW + btnW / 5, self.height / 2 - 20);
    }
    
    
    for (UIButton *btn in self.subviews)
    {
        if (![btn isKindOfClass:[UIControl class]]||btn == self.PublishBtn) continue;
        btnX = btnW * (index > 1 ? (index + 1) : index);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        index++;
        if (index == 3)
        {
            [btn addTarget:self action:@selector(thirdTabbarBtnclick) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
}

-(void)thirdTabbarBtnclick
{
    if (!self.RedView.hidden)
    {
        //点击了消息层
        self.RedView.hidden = YES;
    }
}

@end
